import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    bookData: {
      date: "2016-05-02",
      bookName: "鸟哥的Linux私房菜",
      author: "鸟哥"
    },
    tableData: [
      {
        date: "2016-05-02",
        bookName: "鸟哥的Linux私房菜",
        author: "鸟哥"
      },
      {
        date: "2016-05-04",
        bookName: "深入理解计算机系统（原书第3版）",
        author: "Randal E. Bryant"
      },
      {
        date: "2016-05-01",
        bookName: "UNIX环境高级编程 第3版",
        author: "W. 理查德.史蒂文斯"
      },
      {
        date: "2016-05-03",
        bookName: "Linux内核设计与实现（原书第3版）",
        author: "RobertLove"
      }
    ]
  },
  mutations: {
    delBook(state, bookName) {
      // 变更状态
      const i = state.tableData.findIndex((item) => item.bookName === bookName);
      state.tableData.splice(i, 1);
    },
    toEdit(state, bookName) {
      state.tableData.some((item) => {
        if (item.bookName === bookName) {
          state.bookData = item;
          return true;
        }
      });
    },
    clean(state) {
      state.bookData = { date: "", bookName: "", author: "" };
    }
  },
  actions: {},
  modules: {}
});
